package main

import (
	"fmt"
	"reflect"
)

type Person struct {
	Name  string `json:"name" my:"姓名"`
	Score int    `json:"age" my:"分数"`
}

// 反射机制
func main() {
	p := Person{"tom", 70}
	t := reflect.TypeOf(p)
	fmt.Printf("t.Name(): %v t.Kind():%v\n", t.Name(), t.Kind())
	// 遍历结构体字段信息
	for i := 0; i < t.NumField(); i++ {
		field := t.Field(i)
		fmt.Printf("name:%s index:%d type:%v json tage:%v\n", field.Name, field.Index, field.Type, field.Tag.Get("my"))
	}

	// 通过字段名获取指定结构体字段信息
	if scoreField, ok := t.FieldByName("Score"); ok {
		fmt.Printf(" name:%s index:%d type:%s tag :%s", scoreField.Name, scoreField.Index, scoreField.Type, scoreField.Tag.Get("json"))
	}
}
